package com.toy.abash.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.toy.abash.dao.AbashDAO;
import com.toy.abash.domain.Abash;
import com.toy.abash.service.AbashManager;
import com.toy.abash.service.support.HotType;
import com.toy.base.domain.User;
import com.toy.base.service.UserManager;
import com.toy.core.cache.annotation.CacheEvict;
import com.toy.core.cache.annotation.CachePackage;
import com.toy.core.cache.annotation.CachePutGet;
import com.toy.core.dao.MongoCrudRepository;
import com.toy.core.dao.support.Page;
import com.toy.core.service.impl.BaseManagerImpl;
import com.toy.core.util.DateUtils;
import com.toy.core.util.StringUtils;

@CachePackage(Abash.class)
@Service("abashManager")
public class AbashManagerImpl extends BaseManagerImpl<Abash> implements AbashManager {
	
	@Autowired
	private AbashDAO abashDAO;
	
	@Autowired
	private UserManager userManager;
	
	@CacheEvict
	public void saveAbash(Abash entity,List<MultipartFile> multipartFiles) throws IOException{
		if ( multipartFiles != null && multipartFiles.size() > 0 ){
			List<String> imageIds = new ArrayList<String>(multipartFiles.size());
			for (MultipartFile multipartFile : multipartFiles) {
				String fileName = multipartFile.getOriginalFilename();
				InputStream is =  multipartFile.getInputStream();
				if (StringUtils.isNotBlank(fileName) && is != null ){
					String objectId = abashDAO.saveGridFS(is, fileName);
					imageIds.add(objectId);
				}
			}
			entity.setImages(imageIds.toArray(new String[imageIds.size()]));
		}
		entity.setPublishDate(new Date());
		abashDAO.save(entity);
	}
	
	@CachePutGet
	public Page<Abash> findHotPostsPaged(String hotType,int pageNo, int pageSize){
		Query query = new Query();
		query.addCriteria(Criteria.where("state").is(Abash.STATE_NORMAL));
		HotType type = HotType.valueOf(hotType);
		if( type != null){
			Date[] todayRange = null;
			if ( HotType.dayhot.compareTo(type) == 0){
				 todayRange = DateUtils.getTodayRange();
			}
			if ( HotType.weekhot.compareTo(type) == 0){
				 todayRange = DateUtils.getAwayNowRange(7);
			}
			if ( HotType.monthhot.compareTo(type) == 0){
				 todayRange = DateUtils.getAwayNowRange(30);
			}
			if ( HotType.yearhot.compareTo(type) == 0){
				 todayRange = DateUtils.getAwayNowRange(365);
			}
			if ( todayRange != null ){
				//TIPS:日期查询
				query.addCriteria(Criteria.where("publishDate").gte(todayRange[0]).lte(todayRange[1]));
			}
			
			if ( HotType.allhot.compareTo(type) == 0){
				query.addCriteria(Criteria.where("publishDate").lte(new Date()));
			}
		}
		//TIPS:_id排序
		query.sort().on("hotOrder", Order.DESCENDING).on("_id", Order.DESCENDING);
		return abashDAO.findPaged(query, pageNo, pageSize);
	}
	
	@CachePutGet
	public Page<Abash> findHotImagesPaged(int pageNo, int pageSize){
		Query query = new Query();
		query.addCriteria(Criteria.where("state").is(Abash.STATE_NORMAL));
		query.addCriteria(Criteria.where("images").not().size(0));
		query.sort().on("hotOrder", Order.DESCENDING).on("_id", Order.DESCENDING);
		return abashDAO.findPaged(query, pageNo, pageSize);
	}
	
	@CachePutGet
	public Page<Abash> findNewPostsPaged(int pageNo, int pageSize){
		Query query = new Query();
		query.addCriteria(Criteria.where("state").is(Abash.STATE_NORMAL));
		Date[] todayRange = DateUtils.getAwayNowRange(30);
		query.addCriteria(Criteria.where("publishDate").gte(todayRange[0]).lte(todayRange[1]));
		query.sort().on("_id", Order.DESCENDING);
		return abashDAO.findPaged(query, pageNo, pageSize);
	}
	
	@Override
	public MongoCrudRepository<Abash,Long> getMongoCrudRepository() {
		return abashDAO;
	}

	public Map<Long,User> findPostsUsers(List<Abash> list) {
		if ( list == null || list.size() == 0){
			return Collections.emptyMap();
		}
		Set<Long> userIds = new HashSet<Long>();
		for (Abash abash : list) {
			userIds.add(abash.getAuthorId());
		}
		return userManager.findByUserIds(userIds);
	}


}
